﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Engine;

namespace Engine
{
   public class Lineax
    {
       static Texture2D pixel;
        public static void Initialize(GraphicsDeviceManager graphics)
        {
            pixel = new Texture2D(graphics.GraphicsDevice, 1, 1);//Esto me hace un pixel :D
            Color[] col = new Color[1];
            col[0] = Color.White;
            pixel.SetData<Color>(col);
        }
        public static void DrawLine(SpriteBatch sb, Vector2 posIni, Vector2 posFin)
        {
            Vector2 Screen_pos = posIni;

            double deltX = posFin.X - posIni.X;
            double deltY = posFin.Y - posIni.Y;

            double squaredDetlX = deltX * deltX;
            double squaredDetlY = deltY * deltY;

            double scale = Math.Sqrt(squaredDetlX + squaredDetlY);

            double Rot_Angle = Math.Atan2(deltY, deltX);

            sb.Draw(pixel, Screen_pos, new Rectangle(0, 0, 1, 1), Color.Black, (float)Rot_Angle, Vector2.Zero, new Vector2((float)scale,1.0f), SpriteEffects.None, 1);
        }

        public static void DrawRect(SpriteBatch sb, Vector2 posIni, Vector2 posFin)
        {
            Vector2 esq1 = new Vector2(posFin.X, posIni.Y);
            Vector2 esq2 = new Vector2(posIni.X, posFin.Y);

            DrawLine(sb, posIni, esq1);
            DrawLine(sb, esq1, posFin);
            DrawLine(sb, posFin, esq2);
            DrawLine(sb, esq2, posIni);
        }

        public static void DrawRect(SpriteBatch sb, Rectangle rect)
        {
            Vector2 esq1 = new Vector2(rect.X, rect.Y);
            Vector2 esq2 = new Vector2(rect.X, rect.Y+rect.Height);
            Vector2 esq3 = new Vector2(rect.X + rect.Width, rect.Y + rect.Height);
            Vector2 esq4 = new Vector2(rect.X + rect.Width, rect.Y);

            DrawLine(sb, esq1, esq2);
            DrawLine(sb, esq2, esq3);
            DrawLine(sb, esq3, esq4);
            DrawLine(sb, esq4, esq1);
            
        }

        public static void DrawPolygon(SpriteBatch sb, Vector2[] positions)
        {
            for (int i = 0; i < positions.Length-1; i++)
            {
                DrawLine(sb, positions[i], positions[i + 1]);
            }
        }

        void Draw(Vector2 v1, Vector2 v2)
        {
            double deltax = v2.X - v1.X;
            double deltay = v2.Y - v1.Y;
            double lineasiz = Math.Sqrt(deltax * deltax + deltay * deltay);
            double angle = Math.Atan2(deltay, deltax);
        }
    }
}
